package com.taobao.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.taobao.dao.CommonDao;
import com.taobao.dao.LoginDao;
import com.taobao.pojo.Config;
import com.taobao.pojo.HttpContext;

public class LoginDaoImpl extends CommonDao implements LoginDao {

    @Override
    public boolean login(String userName, String password, String code, String[] ve) {
        boolean rtn = false;
        try {
            Map<String, String> params = new HashMap<String, String>();
            params.put("UserNameTxt", userName);
            params.put("UserPwdTxt", password);
            params.put("CheckCodeTxt", code);
            params.put("__VIEWSTATE", ve[0]);
            params.put("__EVENTVALIDATION", ve[1]);
            params.put("LoginBtn.x", "1");
            params.put("LoginBtn.y", "1");

            String userInfo = (String) this.postRequest(Config.url + "login.aspx", null, params);
            if (userInfo != null && userInfo.contains(userName)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtn;
    }

    @Override
    public Object process(HttpContext context) throws Exception {
        Config.cookieStore = context.getCookieStore();
        Document doc = Jsoup.parse(context.getHtml());
        String userInfo = doc.getElementById("HeadControl1_LoginEdLbl").html();
        return userInfo;
    }
}
